﻿#region

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Pob.CustomEventArgs;
using Pob.GameObjects;

#endregion

namespace Pob.ScreenManagement.GameScreens
{
    public delegate void ScoreScreenEvent(object sender, GameScreenEventArgs args);

    public class ScoreScreen : ScreenBase
    {
        #region Fields

        private readonly MenuControl _control;

        private readonly MenuItem _quit;

        private readonly MenuItem _rematch;

        private readonly List<MenuItem> _selectableItems;

        private readonly DrawString _winnerName;

        private Player _winner;

        #endregion

        #region Constructors and Destructors

        public ScoreScreen(Game game, ScoreScreenEvent eventHandler, Player winner)
            : base(game)
        {
            _winner = winner;

            Background = new Background(game, "PausedScreenBackground", new Vector2(0, 0), 1, Color.White * 0.5f);

            _winnerName = new DrawString(game, GameContent.FontScoreboardPlayer, winner.Name, new Vector2(Settings.Width / 2f, 100), Color.Red, selected: false, scale: 2);

            _rematch = new MenuItem(game, new DrawString(game, GameContent.FontScoreScreenMenuItems, "rematch", new Vector2(Settings.Width / 2f, 320), Color.Orange, selected: true, originMode: OringeMode.MiddleCenter, drawStringMode: DrawStringMode.WithSelected), OnRematchItem);

            _quit = new MenuItem(game, new DrawString(game, GameContent.FontScoreScreenMenuItems, "Menu", new Vector2(Settings.Width / 2f, 360), Color.White, selected: false, originMode: OringeMode.MiddleCenter, drawStringMode: DrawStringMode.WithSelected), OnQuitItem);

            _selectableItems = new List<MenuItem>
                {
                    _rematch, 
                    _quit
                };

            MyComponents.Add(Background);
            MyComponents.Add(_winnerName);
            MyComponents.AddRange(_selectableItems);

            _control = new MenuControl(_selectableItems);

            GameExit += eventHandler;
            GameNew += eventHandler;
        }

        #endregion

        #region Public Events

        public event ScoreScreenEvent GameExit;

        public event ScoreScreenEvent GameNew;

        #endregion

        #region Public Methods and Operators

        public override void Initialize()
        {
            AddRange(MyComponents);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            _control.Update(gameTime);
        }

        #endregion

        #region Methods

        private void OnGameExit()
        {
            var args = new GameScreenEventArgs(null, GameState.Exit);

            if (GameExit != null)
            {
                GameExit(this, args);
            }
        }

        private void OnGameNew()
        {
            var args = new GameScreenEventArgs(null, GameState.New);

            if (GameNew != null)
            {
                GameNew(this, args);
            }
        }

        private void OnQuitItem(object obj, MenuItemPickedEventArgs args)
        {
            OnGameExit();
        }

        private void OnRematchItem(object obj, MenuItemPickedEventArgs args)
        {
            OnGameNew();
        }

        #endregion
    }
}